package com.dashu.device.service;


import com.dashu.base.enums.common.Status;
import com.dashu.device.model.CompanyPersonal;
import com.dashu.device.model.CompanyTeacher;
import com.dashu.device.model.QCompanyPersonal;
import com.dashu.device.model.QCompanyTeacher;
import com.querydsl.core.BooleanBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.FlushModeType;
import javax.transaction.Transactional;

@Slf4j
@Service
public class CompanyPersonalService extends DataService {


    @Autowired
    private DeviceService deviceService;


    @Transactional
    public CompanyPersonal findPersonal(long companyPersonalId) {
        QCompanyPersonal qCompanyPersonal = QCompanyPersonal.companyPersonal;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qCompanyPersonal.id.eq(companyPersonalId));
        CompanyPersonal companyPersonal =
                queryFactory
                        .select(qCompanyPersonal)
                        .from(qCompanyPersonal)
                        .where(booleanBuilder.getValue())
                        .fetchOne();
        return companyPersonal;

    }

    @Transactional
    public void upPersonalStatus(long companyPersonalId) {
        QCompanyPersonal qCompanyPersonal = QCompanyPersonal.companyPersonal;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qCompanyPersonal.id.eq(companyPersonalId));
        CompanyPersonal companyPersonal = queryFactory
                .select(qCompanyPersonal)
                .from(qCompanyPersonal)
                .where(booleanBuilder.getValue())
                .fetchOne();
        companyPersonal.setStatus(Status.REVIEWED);
        entityManager.merge(companyPersonal);
        entityManager.setFlushMode(FlushModeType.AUTO);

    }

}
